package com.hflyeer.controller;


import com.hflyeer.dto.StudentEx;
import com.hflyeer.exception.StudentException;
import com.hflyeer.pojo.ResponseBean;
import com.hflyeer.service.StudentService;
import com.hflyeer.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 学生 前端控制器
 * </p>
 *
 * @author hflyeer
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/student")
@Api(tags = "学生的增删改查")
public class StudentController {

    @Resource
    private StudentService studentService;

    @ApiOperation(value = "添加学生记录", notes = "collegeName,gradeName和collegeId,gradeId不能同传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentNo", value = "学号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "studentName", value = "姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "collegeName", value = "学院名称",  dataType = "String"),
            @ApiImplicitParam(name = "gradeName", value = "班级名称",  dataType = "String"),
            @ApiImplicitParam(name = "collegeId", value = "班级Id",  dataType = "String"),
            @ApiImplicitParam(name = "gradeId", value = "班级Id",  dataType = "String"),
        }
    )
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    protected ResponseBean addStudent(StudentEx stu){
        int add = studentService.addStudent(stu);
        if (add>0)
            return ResponseUtil.success();
        else
            return ResponseUtil.fail(400,false);
    }


    @ApiOperation(notes = "{id}替换成学生id值，该值必须存在", value = "修改学生分数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "score", value = "答题分数", required = true, dataType = "int"),
    }
    )
    @RequestMapping(value = "/update/score/{id}",method = RequestMethod.PUT)
    protected ResponseBean updateScore(@PathVariable("id")Integer id, StudentEx student){
        if (id==null)
            throw new StudentException("请求地址不同");
        int update = studentService.updateScore(student);
        if (update>0)
            return ResponseUtil.success();
        else
            return ResponseUtil.fail(400,false);
    }

}
